package com.newgrand.annexserver.datasource;

import com.newgrand.annexserver.beans.Dbconfig;
import com.newgrand.annexserver.storage.StorageContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author ：minyang
 * @date ：Created in 2019/9/9 9:27
 */
@Aspect
@Component
public class BusinessDataSourceAspect {

    @Pointcut("execution( * com.newgrand.annexserver.mapper.business.*.*(..))")
    public void daoAspect() {
    }

    @Before("daoAspect()")
    public void switchDataSource(JoinPoint point) {

        Dbconfig dbconfig = StorageContextHolder.getDbConfig();
        //设置业务存储上下文
        NgDataSourceContextHolder.setBusinessDataSourceKey(dbconfig);

    }

    @After("daoAspect())")
    public void restoreDataSource(JoinPoint point) {
        NgDataSourceContextHolder.clearDataSourceKey();
    }

}
